* REPLICATION WITH EXTENDED BNOS *
* 1. get extended set of BNOs --> bnoSext_merge.dta

use "$out_HS/bno_fekvo_anon_t.dta", clear
keep anon t fekvo_bno
duplicates drop
gen bno_flagSext=.
foreach x in S03 S13 S23 S33 ///
	S40 S41 S42 S43 S44 S45 S46 S49 ///
	S50 S51 S52 S53 S54 S55 S56 S59 ///
	S60 S61 S62 S63 S64 S65 S66 S69 ///
	S70 S71 S72 S73 S74 S75 S76 S79 ///
	S80 S81 S82 S83 S84 S85 S86 S89 ///
	S90 S91 S92 S93 S94 S95 S96 S99 ///
  {
	gen BNO`x' = .
	replace bno_flagSext = 1 if fekvo_bno=="`x'" 
	replace BNO`x' = 1 if fekvo_bno=="`x'" 
	}
	
egen max_bnoSext = max(bno_flagSext), by(anon t)
foreach var of varlist BNO* {
	egen max_`var' = max(`var'), by(anon t)
	}
	
keep anon t max_bnoSext max_BNO*
duplicates drop anon t, force
sort anon t
egen num_bnoSext = rowtotal(max_BNO*)

lab var 	max_BNOS03	"Dislocation and sprain of joints and ligaments of head	"
lab var 	max_BNOS13	"Dislocation and sprain of joints and ligaments at neck level	"
lab var 	max_BNOS23	"Dislocation and sprain of joints and ligaments of thorax	"
lab var 	max_BNOS33	"Dislocation and sprain of joints and ligaments of lumbar spine and pelvis	"
lab var 	max_BNOS40	"Superficial injury of shoulder and upper arm	"
lab var 	max_BNOS41	"Open wound of shoulder and upper arm	"
lab var 	max_BNOS42	"Fracture of shoulder and upper arm	"
lab var 	max_BNOS43	"Dislocation and sprain of joints and ligaments of shoulder girdle	"
lab var 	max_BNOS44	"Injury of nerves at shoulder and upper arm level	"
lab var 	max_BNOS45	"Injury of blood vessels at shoulder and upper arm level	"
lab var 	max_BNOS46	"Injury of muscle, fascia and tendon at shoulder and upper arm level	"
lab var 	max_BNOS49	"Other and unspecified injuries of shoulder and upper arm	"
lab var 	max_BNOS50	"Superficial injury of elbow and forearm	"
lab var 	max_BNOS51	"Open wound of elbow and forearm	"
lab var 	max_BNOS52	"Fracture of forearm	"
lab var 	max_BNOS53	"Dislocation and sprain of joints and ligaments of elbow	"
lab var 	max_BNOS54	"Injury of nerves at forearm level	"
lab var 	max_BNOS55	"Injury of blood vessels at forearm level	"
lab var 	max_BNOS56	"Injury of muscle, fascia and tendon at forearm level	"
lab var 	max_BNOS59	"Other and unspecified injuries of elbow and forearm	"
lab var 	max_BNOS60	"Superficial injury of wrist, hand and fingers	"
lab var 	max_BNOS61	"Open wound of wrist, hand and fingers	"
lab var 	max_BNOS62	"Fracture at wrist and hand level	"
lab var 	max_BNOS63	"Dislocation and sprain of joints and ligaments at wrist and hand level	"
lab var 	max_BNOS64	"Injury of nerves at wrist and hand level	"
lab var 	max_BNOS65	"Injury of blood vessels at wrist and hand level	"
lab var 	max_BNOS66	"Injury of muscle, fascia and tendon at wrist and hand level	"
lab var 	max_BNOS69	"Other and unspecified injuries of wrist, hand and finger(s)	"
lab var 	max_BNOS70	"Superficial injury of hip and thigh	"
lab var 	max_BNOS71	"Open wound of hip and thigh	"
lab var 	max_BNOS72	"Fracture of femur	"
lab var 	max_BNOS73	"Dislocation and sprain of joint and ligaments of hip	"
lab var 	max_BNOS74	"Injury of nerves at hip and thigh level	"
lab var 	max_BNOS75	"Injury of blood vessels at hip and thigh level	"
lab var 	max_BNOS76	"Injury of muscle, fascia and tendon at hip and thigh level	"
lab var 	max_BNOS79	"Other and unspecified injuries of hip and thigh	"
lab var 	max_BNOS80	"Superficial injury of knee and lower leg	"
lab var 	max_BNOS81	"Open wound of knee and lower leg	"
lab var 	max_BNOS82	"Fracture of lower leg, including ankle	"
lab var 	max_BNOS83	"Dislocation and sprain of joints and ligaments of knee	"
lab var 	max_BNOS84	"Injury of nerves at lower leg level	"
lab var 	max_BNOS85	"Injury of blood vessels at lower leg level	"
lab var 	max_BNOS86	"Injury of muscle, fascia and tendon at lower leg level	"
lab var 	max_BNOS89	"Other and unspecified injuries of lower leg	"
lab var 	max_BNOS90	"Superficial injury of ankle, foot and toes	"
lab var 	max_BNOS91	"Open wound of ankle, foot and toes	"
lab var 	max_BNOS92	"Fracture of foot and toe, except ankle	"
lab var 	max_BNOS93	"Dislocation and sprain of joints and ligaments at ankle, foot and toe level	"
lab var 	max_BNOS94	"Injury of nerves at ankle and foot level	"
lab var 	max_BNOS95	"Injury of blood vessels at ankle and foot level	"
lab var 	max_BNOS96	"Injury of muscle and tendon at ankle and foot level	"
lab var 	max_BNOS99	"Other and unspecified injuries of ankle and foot	"

foreach x in S03 S13 S23 S33 ///
	S40 S41 S42 S43 S44 S45 S46 S49 ///
	S50 S51 S52 S53 S54 S55 S56 S59 ///
	S60 S61 S62 S63 S64 S65 S66 S69 ///
	S70 S71 S72 S73 S74 S75 S76 S79 ///
	S80 S81 S82 S83 S84 S85 S86 S89 ///
	S90 S91 S92 S93 S94 S95 S96 S99 /// 
	{
		rename max_BNO`x' BNO`x'
		}
		
lab var num_bnoSext "# of bnoSext shocks"

*save "$out/bnoSext_merge.dta", replace
tempfile bnoSext_merge
save `bnoSext_merge'

* 2. PLACEBOS --> placebo_base.dta
use anon t vallazon1 ev ho w1 kor ferfi fogvisz1 feor1 wh1 transfer_mtp id_tip wtip1 jnap if (kor>=20 & kor<=50) & (ev>=2009 & ev<=2017) using "$in/admin3_alap.dta", clear

#delimit ;
label define lid_tip 
0 "no firm in t" 
1 "1 firm on 15th of t" 
2 "2 firms on 15th of t" 
3 ">2 firms on 15th of t" 
4 "1 firm on 15th of t + non15" 
5 "2 firms on 15th of t + non15" 
6 "only non15 firms in t", replace
;
#delimit cr
label values id_tip lid_tip		
label define lwtip 0 "monthly" 1 "smoothed" 2 "mixed"
label values wtip lwtip 

rename wh1 munkaido1

merge 1:1 anon t using "$in_med/admin3_eu_veny_H2_v2.dta", keep(1 3)
*if any information about byuing any social security supported medicaments that month
gen never_took_prescriptions=1 if _merge==1
replace never_took_prescriptions=0 if _merge==3
foreach var of varlist A_ft-R03_ft{
replace `var'=0 if `var'==. & _merge==3
}
*if buying any social security supported medicaments that month
gen no_prescriptions=1 
foreach var of varlist A_ft-R03_ft{
	replace no_prescriptions=0 if `var'>0 & _merge==3
}
drop _merge

* occupation *
cap drop occupation
gen occupation=. 
replace occupation=0 if missing(feor1_h2)
replace occupation=3 if feor1_h2==11
replace occupation=4 if feor1_h2==12
replace occupation=7 if feor1_h2==13 
replace occupation=1 if feor1_h2==14
replace occupation=1 if feor1_h2==15
replace occupation=3 if feor1_h2>=17 & feor1_h2<=23
replace occupation=4 if feor1_h2>=24 & feor1_h2<=33
replace occupation=5 if feor1_h2>=34 & feor1_h2<=43
replace occupation=6 if feor1_h2>=44 & feor1_h2<=47
replace occupation=7 if feor1_h2==48

#delimit ;
lab def occupation
0 "No info"
1 "Manager, political/religious/ngo leader"
3 "Professional"
4 "Other white collar"
5 "Skilled blue collar"
6 "Assembler, machine op"
7 "Unskilled laborer", replace
;
#delimit cr
lab val occupation occupation 

*keep if (kor<=50 & kor>=20) & (ev>=2009 & ev<=2017)

gen employed=0
replace employed=1 if fogvisz1~=.

* create a winsorized version of the wage
gen w1_wins=w1
egen wp1=pctile(w1) if employed==1, by(t) p(1)
egen wp99=pctile(w1) if employed==1, by(t) p(99)
replace w1_wins=wp1 if w1_wins<wp1 & w1_wins!=0 & employed==1
replace w1_wins=wp99 if w1_wins>wp99 & w1_wins!=. & employed==1
drop wp1 wp99

foreach X in "" _wins{
	gen lnw`X'=log(w1`X'/((munkaido1*30)/7))
	gen lnw`X'_restr = lnw`X'
		replace lnw`X'_restr = . if transfer_mtp==401

	* hourly wage calculated based on precise days worked (when wage is paid)
	gen lnw`X'_prec=log(w1`X'/(munkaido1*jnap))
	gen lnw`X'_prec_restr = lnw`X'_prec
		replace lnw`X'_prec_restr = . if transfer_mtp==401
	label var lnw`X' "log wage"
	label var lnw`X'_restr "log wage taking out months with sickness benefit"
	label var lnw`X'_prec "log wage using prcise hours worked"
	label var lnw`X'_prec_restr "log precise wage taking out months with sickness benefit"
}

sum lnw, det
gen lnw_outl = 1 if (lnw<`r(p1)' | lnw>`r(p99)') & lnw!=. 
	recode lnw_outl (.=0)
sum lnw_prec, det
gen lnw_prec_outl = 1 if (lnw_prec<`r(p1)' | lnw_prec>`r(p99)') & lnw_prec!=. 
	recode lnw_prec_outl (.=0)
label var lnw_outl "indicator for top/bottom 1% hourly wage outliers"
label var lnw_prec_outl "indicator for top/bottom 1% precise hourly wage outliers"
			
rename munkaido1 munkaido

cap drop _merge
merge 1:1 anon t using `bnoSext_merge' 
drop if _m==2
drop _merge
sort anon t

recode max_bnoSext (.=0)

merge 1:1 anon t using "$in/admin3_eu_fekvo.dta", keep(master match) nogen keepusing(korhazi_nap) 

gen hosp5 = 1 if korhazi_nap>=5 & korhazi_nap~=.
	recode hosp5 (.=0)

drop vallazon1 ev ho w1 w1_wins kor ferfi fogvisz1 feor1 transfer_mtp mo_megye num_bnoSext BNOS?? korhazi_nap
compress

save "$out/placebo_base_bnoSext_2023Dec.dta", replace

